﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.DataStruct;

namespace ConsoleApp2
{
    internal class TestTimingTask
    {
        public static void Test()
        {
            //TestXX();
            TestDemo();
        }

        private static void TestXX()
        {
            List<CallbackExcuteTimingTask> list = new List<CallbackExcuteTimingTask>()
            {
                 new CallbackExcuteTimingTask(Callback,  TimeSpan.FromMilliseconds(1000), 5) { Tag = 1 },
                 new CallbackExcuteTimingTask(Callback,  TimeSpan.FromMilliseconds(2000), 2) { Tag = 2 },
                 new CallbackExcuteTimingTask(Callback,  TimeSpan.FromMilliseconds(3000)) { Tag = 3 },
                 new CallbackExcuteTimingTask(Callback,  TimeSpan.FromMilliseconds(3100)) { Tag = 4 },
            };

            var firstItem = list.First();
            var ret1 = list.Remove(firstItem);
            var ret2 = list.Remove(firstItem);

            //foreach(var item in list)
            //{
            //    list.Remove(item);
            //}

            CallbackExcuteTimingTask item;
            for (int i = 0; i < list.Count;)
            {
                item = list[i];


                list.RemoveAt(i);
                if (list.Count > i && item == list[i])
                {
                    i++;
                }
            }


        }

        private static void TestDemo()
        {
            TimingTaskManager.Default.AddTask(new CallbackExcuteTimingTask(Callback, TimeSpan.FromMilliseconds(1000)) { Name = "间隔1s" });
            TimingTaskManager.Default.AddTask(new CallbackExcuteTimingTask(Callback, TimeSpan.FromMilliseconds(2000), 3) { Name = "间隔2s" });
            TimingTaskManager.Default.AddTask(new CallbackExcuteTimingTask(Callback, TimeSpan.FromMilliseconds(3000)) { Name = "间隔3s" });
            TimingTaskManager.Default.AddTask(new CallbackExcuteTimingTask(Callback, TimeSpan.FromMilliseconds(3500), 5) { Name = "间隔4s" });
            TimingTaskManager.Default.AddTask(new CallbackExcuteTimingTask(Callback, 15, 37, 10, 0) { Name = "15:37:10. 0" });
        }

        private static Dictionary<string, StringBuilder> _dic = new Dictionary<string, StringBuilder>();

        private static void Callback(CallbackExcuteTimingTask timingTask)
        {
            string msg = $"{DateTime.Now.ToString("mm:ss.fffffff")}         {timingTask.Name}";
            Console.WriteLine(msg);

            StringBuilder sb;
            if (!_dic.TryGetValue(timingTask.Name, out sb))
            {
                sb = new StringBuilder();
                _dic.Add(timingTask.Name, sb);
            }
            sb.AppendLine(msg);
        }

        public static string GetTimeInfo()
        {
            TimingTaskManager.Default.Clear();
            StringBuilder sb = new StringBuilder();
            foreach (var kv in _dic)
            {
                if (sb.Length > 0)
                {
                    sb.AppendLine("****************************");
                }
                sb.Append(kv.Value.ToString());
            }
            string str = sb.ToString();

            Thread.Sleep(100);

            return str;
        }
    }
}
